/**
 * 
 */
package jMiiEditor.tools;

import java.util.List;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import cx.ath.choisnet.io.FileIterator;

/**
 * @author Claude
 *
 */
public class CheckJarManifest 
{
    private JarFile      jarFile;
    private List<String> fileNameList = new ArrayList<String>();
    
    public CheckJarManifest( File file ) throws IOException
    {
        try {
            jarFile = new JarFile( file );
        }
        catch( IOException e ) {
            throw new IOException( file.toString(), e );
        }
    }

    public void checkJar() throws IOException
    {
        Enumeration<JarEntry> entries = jarFile.entries();
        int                   count   = 0;
        
        while( entries.hasMoreElements() ) {
            JarEntry je = entries.nextElement();
            
            if( !je.isDirectory() ) {
                count++;
                fileNameList.add( je.getName() );
            }
            // System.out.println(" >" + je.getName() );
        }
        System.out.println("Jar has " + count + " items"); 
        
        checkManifest( jarFile.getManifest() );
        
        for(String n : this.fileNameList ) {
            System.out.println(" <> " + n ); 
        }
    }
    
    private void checkManifest( Manifest manifest )
    {
        Set<Entry<String,Attributes>> set = manifest.getEntries().entrySet();
        
        System.out.println("Manifest has " + set.size() + " items"); 

        int    count   = 0;
        String prevKey = null;
        
        for( Iterator<Map.Entry<String,Attributes>> iterator = set.iterator(); iterator.hasNext(); ) {
            Map.Entry<String,Attributes> entry = iterator.next();
            String                       key = entry.getKey();
            
            count++;
            
            if( jarFile.getEntry( key ) == null ) {
                System.err.println( "Err:" + jarFile.getName() + " key=" + key + " entry # " + count + " (" + (count*3) + ") prevKey = " + prevKey );
            }
            else {
//                if( key.startsWith( "jMiiEditor" )) {
//                    Attributes a = entry.getValue();
//                    System.out.println( "OK: key=" + key + " - " + a );
//                    for( Map.Entry<Object, Object> x : a.entrySet() ) {
//                        System.out.println( "  > key=" + x.getKey() + " - value=" + x.getValue() );                  
//                    }
//                }
                this.fileNameList.remove( key );
            }
            prevKey = key;
        }
    }

    /**
     * @param args
     * @throws IOException 
     */
    public static void main( String[] args ) throws IOException
    {
        File root = new File("ant-dist").getAbsoluteFile();

        FileIterator iter = new FileIterator(
                root,
                new FileFilter()
                {
                    @Override
                    public boolean accept( File file )
                    {
                        return file.getName().endsWith( ".jar" );
                    }
                });
        
        for( File f : iter ) {
            System.out.println("reading: " + f);

            CheckJarManifest checker = new CheckJarManifest( f );
            checker.checkJar();
        }
    }


}
